package com.ymatou.iapi.productmanager.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.deliveryTemplate.TemplateProductCountBean;
import com.ymatou.iapi.productmanager.parameter.deliveryTemplate.TemplateProductInfoDto;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.parameter.product.ProductFreightSaveModel;
import com.ymatou.iapi.productmanager.service.AddSellerProductCall;
import com.ymatou.iapi.productmanager.service.GetTemplateProductCountCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddProductCallService;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.compare.AssertHelper;

public class Ts_GetTemplateProductCount {
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	private static GetTemplateProductCountCall getTemplateProductCountCall = new GetTemplateProductCountCall();
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("取运费模板关联的商品数据");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getTemplateProductCountCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateProductCount_001() {
		Logger.start(true, "取运费模板关联的商品数据 全量数据");
		try{
			int templateId = 3123;
			intergratedProductMapper.deleteDeliveryTemplateBySellerIdAndTemplateId(sellerId, templateId);
			
			List<String> productList = new ArrayList<>();
			for(int i=0;i<5;i++){
				AddSellerProductRequest addProdReq = AddProductCallService.getAddSellerProductRequestFromFile("多规格", 
					MapUtil.hashMap("sellerId",sellerId,"sellerName",sellerName));
				
				ProductFreightSaveModel  freightModel = new ProductFreightSaveModel();
				freightModel.setCatalogStatus(3);
				freightModel.setTemplateId(templateId);
				addProdReq.getProduct().setFreightTemplateList(Arrays.asList(freightModel));
				
				JSONObject ret = new AddSellerProductCall().callAndGetReturnData(addProdReq);
				
				productList.add(ret.getJSONObject("Data").getString("ProductId"));
			}
			
			//isshow = true
			JSONObject ret = getTemplateProductCountCall.call(sellerId, templateId, true);
			checkGetTemplateProductCount(true,productList,ret);
			
			//isshow = false
			JSONObject ret1 = getTemplateProductCountCall.call(sellerId, templateId, false);
			checkGetTemplateProductCount(false,productList,ret1);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateProductCount_002() {
		Logger.start(true, "取运费模板关联的商品数据 全量数据 ,删除商品无法统计在内");
		try{
			int templateId = 3123;
			intergratedProductMapper.deleteDeliveryTemplateBySellerIdAndTemplateId(sellerId, templateId);
			
			List<String> productList = new ArrayList<>();
			for(int i=0;i<5;i++){
				AddSellerProductRequest addProdReq = AddProductCallService.getAddSellerProductRequestFromFile("多规格", 
					MapUtil.hashMap("sellerId",sellerId,"sellerName",sellerName));
				
				ProductFreightSaveModel  freightModel = new ProductFreightSaveModel();
				freightModel.setCatalogStatus(3);
				freightModel.setTemplateId(templateId);
				addProdReq.getProduct().setFreightTemplateList(Arrays.asList(freightModel));
				
				JSONObject ret = new AddSellerProductCall().callAndGetReturnData(addProdReq);
				
				productList.add(ret.getJSONObject("Data").getString("ProductId"));
			}
			
			
			SellerProductCallService.batchDeleteProduct(sellerId, Arrays.asList(productList.get(3)));
			productList.remove(productList.get(3));
			
			//isshow = true
			JSONObject ret = getTemplateProductCountCall.call(sellerId, templateId, true);
			checkGetTemplateProductCount(true,productList,ret);
			
			//isshow = false
			JSONObject ret1 = getTemplateProductCountCall.call(sellerId, templateId, false);
			checkGetTemplateProductCount(false,productList,ret1);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateProductCount_003() {
		Logger.start(true, "取运费模板关联的商品数据 没有运费模板sellerId查询");
		try{
			int templateId = 3123;
			intergratedProductMapper.deleteDeliveryTemplateBySellerIdAndTemplateId(sellerId, templateId);
			
			
			
			//isshow = true
			JSONObject ret = getTemplateProductCountCall.call(sellerId, templateId, true);
			checkGetTemplateProductCount(true,new ArrayList<>(),ret);
			
			//isshow = false
			JSONObject ret1 = getTemplateProductCountCall.call(sellerId, templateId, false);
			checkGetTemplateProductCount(false,new ArrayList<>(),ret1);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateProductCount_004() {
		Logger.start(true, "取运费模板关联的商品数据  不存在templateId");
		try{
			int templateId = 3123;
			intergratedProductMapper.deleteDeliveryTemplateBySellerIdAndTemplateId(sellerId, templateId);
			
			List<String> productList = new ArrayList<>();
			for(int i=0;i<5;i++){
				AddSellerProductRequest addProdReq = AddProductCallService.getAddSellerProductRequestFromFile("多规格", 
					MapUtil.hashMap("sellerId",sellerId,"sellerName",sellerName));
				
				ProductFreightSaveModel  freightModel = new ProductFreightSaveModel();
				freightModel.setCatalogStatus(3);
				freightModel.setTemplateId(templateId);
				addProdReq.getProduct().setFreightTemplateList(Arrays.asList(freightModel));
				
				JSONObject ret = new AddSellerProductCall().callAndGetReturnData(addProdReq);
				
				productList.add(ret.getJSONObject("Data").getString("ProductId"));
			}
			
			
			
			//isshow = true
			JSONObject ret = getTemplateProductCountCall.call(sellerId, templateId+1, true);
			checkGetTemplateProductCount(true,new ArrayList<>(),ret);
			
			//isshow = false
			JSONObject ret1 = getTemplateProductCountCall.call(sellerId, templateId+1, false);
			checkGetTemplateProductCount(false,new ArrayList<>(),ret1);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateProductCount_005() {
		Logger.start(true, "取运费模板关联的商品数据  不存在sellerId");
		try{
			int templateId = 3123;
			intergratedProductMapper.deleteDeliveryTemplateBySellerIdAndTemplateId(sellerId, templateId);
			
			List<String> productList = new ArrayList<>();
			for(int i=0;i<5;i++){
				AddSellerProductRequest addProdReq = AddProductCallService.getAddSellerProductRequestFromFile("多规格", 
					MapUtil.hashMap("sellerId",sellerId,"sellerName",sellerName));
				
				ProductFreightSaveModel  freightModel = new ProductFreightSaveModel();
				freightModel.setCatalogStatus(3);
				freightModel.setTemplateId(templateId);
				addProdReq.getProduct().setFreightTemplateList(Arrays.asList(freightModel));
				
				JSONObject ret = new AddSellerProductCall().callAndGetReturnData(addProdReq);
				
				productList.add(ret.getJSONObject("Data").getString("ProductId"));
			}
			
			
			
			//isshow = true
			JSONObject ret = getTemplateProductCountCall.call(sellerId+1, templateId, true);
			checkGetTemplateProductCount(true,new ArrayList<>(),ret);
			
			//isshow = false
			JSONObject ret1 = getTemplateProductCountCall.call(sellerId+1, templateId, false);
			checkGetTemplateProductCount(false,new ArrayList<>(),ret1);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	public static void checkGetTemplateProductCount(boolean isShow,List<String> prodList,JSONObject ret) throws JSONException{
		Logger.verifyEquals(200, ret.getInt("Code"), "Check return code");
		TemplateProductCountBean actBean = ProductManagerCallService.getBeanFromJson(ret.getJSONObject("Data"), TemplateProductCountBean.class);
		TemplateProductCountBean tgtBean = getTemplateProductCountBean(isShow,prodList);
		AssertHelper.assertResultEqual(tgtBean, actBean, "checkGetTemplateProductCount");
	
	}
	
	public static TemplateProductCountBean getTemplateProductCountBean(boolean isShow,List<String> prodList){
		TemplateProductCountBean tgtBean = new TemplateProductCountBean();
		List<TemplateProductInfoDto> tmpProdList = new ArrayList<>();
		
		if (isShow&&prodList.size()>0){
			for(String prod:prodList){
				TemplateProductInfoDto tmpProd = new TemplateProductInfoDto();
				tmpProd.setProductId(prod);
				tmpProd.setProductName(intergratedProductMapper.selectProductByProdId(prod).get("sProduct").toString());
				tmpProdList.add(tmpProd);
			}
			tgtBean.setProductList(tmpProdList);
		}else{
			tgtBean.setProductList(null);
		}
		
		
		tgtBean.setProductCount(prodList.size());
		return tgtBean;
	}
	
}
